// pages/pre/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hzqingk: '',
    bingzs: '',
    region: ["四川省", "广元市", "苍溪县"],
    img: [],
    data_arr: [],
    items: [{
        name: '冠心病',
        value: '冠心病'
      },
      {
        name: '高血压',
        value: '高血压'
      },
      {
        name: '糖尿病',
        value: '糖尿病'
      }
    ],
    sick_items: [

    ],
    rad_items: [{
        name: 'USA',
        value: '去专家所在医院手术',
        desc: '遵从专家医院制度,等待床位'
      },
      {
        name: 'CHN',
        value: '去专家所在医院手术',
        desc: '遵从专家医院制度,等待床位'
      },
      {
        name: 'BRA',
        value: '去专家所在医院手术',
        desc: '遵从专家医院制度,等待床位'
      },
      {
        name: 'JPN',
        value: '去专家所在医院手术',
        desc: '遵从专家医院制度,等待床位'
      }
    ],
    sickm_key: true,
    textarea: '',
    pagescroll: false,
    textkey: false
  },
  // 复选框
  checkboxChange: function(e) {
    var hzqingk = e.detail.value
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      hzqingk: hzqingk
    })
  },
  //疾病复选框
  sickChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      hzqingk: e.detail.value
    })
  },
  //手术地点单选框
  radioChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  // 地区选择器
  changeRegin(e) {
    console.log(e)
    this.setData({
      region: e.detail.value
    });
  },

  // 上传图片
  uploadimg: function() {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.data_arr;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function(res) {
              console.log(res)
              data_arr.push(res.data)
              wx.setStorageSync('img_arr', data_arr)
              that.setData({
                img: data_arr
              })
            }
          })
        }
      }
    })
  },
  // 删除图片
  downimg: function(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.data.img.splice(index, 1);
    this.setData({
      img: this.data.img
    })
  },
  showsickm: function(e) {
    this.setData({
      textarea: "",
      sickm_key: false,
      pagescroll: true,
      textkey: true
    })
  },
  // 删除疾病
  del: function(e) {
    let index = e.currentTarget.dataset.index;
    this.data.sick_items.splice(index, 1);
    this.setData({
      sick_items: this.data.sick_items
    })
  },
  //获取表单内容
  getarea: function(e) {
    this.setData({
      textarea: e.detail.value
    })
  },
  // 添加疾病
  sickmask: function(e) {
    if (this.data.textarea == "") {
      wx.showToast({
        title: '不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        sickm_key: true,
        pagescroll: false
      })
      let index = e.currentTarget.dataset.index;
      if (index == 1) {
        let obj = {
          name: this.data.textarea,
          value: this.data.textarea,
          checked: true
        }
        this.data.sick_items.push(obj)
        this.setData({
          sick_items: this.data.sick_items,
          textkey: false
        })
      }
    }

  },
  //取消选中
  center: function(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let check = `sick_items[${index}].checked`
    this.setData({
      [check]: !this.data.sick_items[index].checked
    })
  },
  sickmask2() {
    this.setData({
      sickm_key: true,
      pagescroll: false,
      textkey: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var zid = options.zid
    var money = options.money
    var key = options.key
    var name = options.name
    app.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data,
          bgc: bgc,
          zid: zid,
          money: money,
          key: key,
          name: name
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.docinfo',
      data: {
        zid: zid,
        key: options.key,
      },
      success: function(res) {
        that.setData({
          newmoney: res.data.data.newmoney,
          z_name: res.data.data.z_name,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.names) {
      that.setData({ //将携带的参数赋值
        names: currPage.data.names
      });
    }
    if (currPage.data.j_id) {
      that.setData({ //将携带的参数赋值
        j_id: currPage.data.j_id
      });
    }
    if (currPage.data.tel) {
      that.setData({ //将携带的参数赋值
        tel: currPage.data.tel
      });
    }
    if (currPage.data.region && currPage.data.state == 1) {
      that.setData({ //将携带的参数赋值
        region: currPage.data.region
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  selectuser(e) {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi'
    })
  },
  bingzs(e) {
    var that = this
    var bingzs = e.detail.value
    that.setData({
      bingzs: bingzs
    })
  },
  formSubmit(e) {
    var that = this
    var region = that.data.region //城市
    var hospitalname = e.detail.value.hospitalname //在哪家医院
    var names = e.detail.value.names //患者姓名
    var othersick = e.detail.value.othersick //病情主诉
    var tel = e.detail.value.tel //电话
    var img = that.data.img //图片
    var name = that.data.name//服务类型
    var zid = that.data.zid
    if (names == '') {
      wx.showToast({
        title: '请选择患者',
        icon: 'none'
      })
      return false
    }
    if (that.data.sick_items == '') {
      wx.showToast({
        title: '请添加疾病诊断',
        icon: 'none'
      })
      return false
    } else {
      var sick_items = that.data.sick_items //疾病诊断
      var str = []
      for (var i = 0; i < sick_items.length; ++i) {
        str.push(sick_items[i].value)
      }
      var sick_itemsbk = str.join('　');
      console.log(sick_itemsbk)
    }
    if (img == '') {
      wx.showToast({
        title: '至少添加一张图片',
        icon: 'none'
      })
      return false
    } else {
      var imgs = JSON.stringify(img)
    }

    var bingzs = that.data.bingzs

    if (that.data.hzqingk == '') {
      var hzqingkbak = "" //患者其他情况
    } else {
      console.log('2')
      var hzqingk = that.data.hzqingk //患者其他情况
      var hzqingkbak = hzqingk.join('　')
    }
    var regionbak = region.join('　')
    var z_name = that.data.z_name //医生姓名
    var mesage = [{
        question: '患者姓名',
        ansers: names
      },
      {
        question: '联系方式',
        ansers: tel
      },
      {
        question: '所在城市',
        ansers: regionbak
      },
      {
        question: '疾病诊断',
        ansers: sick_itemsbk
      },
      {
        question: '病情主诉',
        ansers: bingzs
      },
      {
        question: '在哪家医院确诊需要手术',
        ansers: hospitalname
      },
      {
        question: '术前资料',
        ansers: '',
        img: imgs
      },
      {
        question: '患者其他情况',
        ansers: hzqingkbak
      },
      {
        question: '其他慢性病',
        ansers: othersick
      },

    ]
    app.util.request({
      'url': 'entry/wxapp/Shoushu.addshoushu',
      data: {
        openid: wx.getStorageSync('openid'),
        mesage: mesage,
        j_id: that.data.j_id
      },
      success: function(res) {
        console.log(res)
        var bl_id = res.data
        //插入问诊订单
        var name = that.data.name
        var keywords = that.data.key
        var money = that.data.money
        app.util.request({
          'url': 'entry/wxapp/Hzbingli.addorder',
          data: {
            zid: zid,
            name: name,
            keywords: keywords,
            openid: wx.getStorageSync('openid'),
            bl_id: bl_id,
            money: money,
            j_id: that.data.j_id
          },
          success: function(res) {
            console.log(res)
            var name = res.data.name
            var order = res.data.order
            var money = res.data.money
            var bingzs = that.data.bingzs
            var oid = res.data.oid
            wx.navigateTo({
              url: '/hyb_yl/czhuanjiasubpages/pages/tijiandetail/index?name=' + name + '&order=' + order + '&money=' + money + '&z_name=' + z_name + '&usernames=' + names + '&num=4' + '&bingzs=' + bingzs + '&zid=' + zid + '&oid=' + oid + '&bl_id=' + bl_id,
            })
          }
        })
      }
    })
  }
})